package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import oracle.jdbc.pool.OracleDataSource;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author BAGUS
 */
public class Pemesanan {

    private String kodePemesanan;
    private String waktuPemesanan;
    private double jumlahGasElpiji;
    private String jenisGasElpiji;
    private String username;
    private String status;
    private String kodeUpdate;
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:XE";
    String userid = "hr"; // gantikan dengan username anda
    String pass = "hr"; // gantikan dengan password anda

    public String getKodePemesanan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select kodePemesanan from Pemesanan order by kodePemesanan");
            String kodePemesanan1 = "";
            Date now = new Date();
            SimpleDateFormat dateFormatterTanggal = new SimpleDateFormat("d");
            SimpleDateFormat dateFormatterBulan = new SimpleDateFormat("M");
            SimpleDateFormat dateFormatterTahun = new SimpleDateFormat("y");
            String tanggal = dateFormatterTanggal.format(now);
            String bulan = dateFormatterBulan.format(now);
            String tahun = dateFormatterTahun.format(now);
            String substring = tahun.substring(2);
            String kodeTanggal = tanggal + bulan + substring;
            while (resultSet.next()) {
                kodePemesanan1 = resultSet.getString("kodePemesanan");
            }
            int no_Pemesanan = 0;
            if ("".equals(kodePemesanan1)) {
                this.kodePemesanan = kodeTanggal + "201";
            } else {
                String data = null;
                if (kodePemesanan1.length() == 9) {
                    data = kodePemesanan1.substring(0, 6);
                } else if (kodePemesanan1.length() == 8) {
                    data = kodePemesanan1.substring(0, 5);
                }
                if (kodeTanggal.equals(data)) {
                    no_Pemesanan = Integer.parseInt(kodePemesanan1) + 1;
                    this.kodePemesanan = String.valueOf(no_Pemesanan);
                } else {
                    this.kodePemesanan = kodeTanggal + "201";
                }
            }
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            this.kodePemesanan = null;
        }
        return kodePemesanan;
    }

    public String getWaktuPemesanan() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d-M-y");
        this.waktuPemesanan = simpleDateFormat.format(date);
        return waktuPemesanan;
    }

    public void setJumlahGasElpiji(double jumlahGasElpiji) {
        this.jumlahGasElpiji = jumlahGasElpiji;
    }

    public double getJumlahGasElpiji() {
        return jumlahGasElpiji;
    }

    public void setJenisGasElpiji(String jenisGasElpiji) {
        this.jenisGasElpiji = jenisGasElpiji;
    }

    public String getJenisGasElpiji() {
        return jenisGasElpiji;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStatus() {
        return status;
    }

    public void setKodeUpdate(String kodeUpdate) {
        this.kodeUpdate = kodeUpdate;
    }

    public String getKodeUpdate() {
        return kodeUpdate;
    }

    public void insertPemesanan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            if ("belum".equals(getStatus())) {
                statement.executeQuery("insert into pemesanan values ('"
                        + getKodePemesanan() + "','"
                        + getWaktuPemesanan() + "',"
                        + getJumlahGasElpiji() + ",'"
                        + getJenisGasElpiji() + "','"
                        + getUsername() + "','"
                        + getStatus() + "')");
            } else {
                statement.executeUpdate("update pemesanan"
                        + " set status = 'terkirim'"
                        + " where kodepemesanan = '" + getKodeUpdate() + "'");
            }
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
    }

    public ResultSet tampilPemesanan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from pemesanan");
            return resultSet;
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return null;
        }
    }
    
    public ResultSet tampilKodePemesanan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select kodepemesanan from pemesanan where status = 'belum'");
            return resultSet;
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return null;
        }
    }
}
